package com.csp.resulthandle;

import com.csp.anotation.mng.MethodMapping;
import com.csp.session.ExecuteMsg;
import com.csp.session.impl.ResourceWrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author:chengsp
 * @createTime:2019/8/23 10:29
 */
public class BaseTypeHandle implements ResultHandle {
    private final static ResultHandle resultHandle = new BaseTypeHandle();

    private BaseTypeHandle() {
    }

    public static ResultHandle getInstance() {
        return resultHandle;
    }

    private final static Logger logger = LoggerFactory.getLogger(BaseTypeHandle.class);

    @Override
    public Object execute(PreparedStatement statement, ExecuteMsg executeMsg, ResourceWrap resourceWrap) {
        try {
            resourceWrap.addStatement(statement);
            MethodMapping methodMapping = executeMsg.getMethodMapping();
            logger.info("预编译sql:{}", methodMapping.getPreparedSql());
            executeMsg.setParams(statement);
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
